package com.kang.util;


import java.io.Serializable;

public class WebResponse implements Serializable {

	private String code;		// 前端通过code来判断这次请求是否成功
	private String msg;
	private Object data;
	
	public String getCode() {
		return code;
	}
	public void setCode(String code) {
		this.code = code;
	}

	public String getMsg() {
		return msg;
	}

	public void setMsg(String msg) {
		this.msg = msg;
	}

	public Object getData() {
		return data;
	}

	public void setData(Object data) {
		this.data = data;
	}

	public static WebResponse resSuccess(String codeMsg, Object data) {
		WebResponse res = new WebResponse();
		res.setCode(ResCode.SUCCESS);
		if (codeMsg == null || "".equals(codeMsg.trim())) {
			res.setMsg("查询成功");
		} else {
			res.setMsg(codeMsg);
		}
		res.setData(data);
		return res;
	}

	public static WebResponse resFail(String codeMsg, Object data) {
		WebResponse res = new WebResponse();
		res.setCode(ResCode.FAIL);
		if (codeMsg == null || "".equals(codeMsg.trim())) {
			res.setMsg("查询失败");
		} else {
			res.setMsg(codeMsg);
		}
		res.setData(data);
		return res;
	}

	public static WebResponse resWarn(String codeMsg, Object data) {
		WebResponse res = new WebResponse();
		res.setCode(ResCode.WARN);
		if (codeMsg == null || "".equals(codeMsg.trim())) {
			res.setMsg("警告提示");
		} else {
			res.setMsg(codeMsg);
		}
		res.setData(data);
		return res;
	}

	public static WebResponse resException(String codeMsg, Object data) {
		WebResponse res = new WebResponse();
		res.setCode(ResCode.EXCEPTION);
		if (codeMsg == null || "".equals(codeMsg.trim())) {
			res.setMsg("系统异常");
		} else {
			res.setMsg(codeMsg);
		}
		res.setData(data);
		return res;
	}
	public static WebResponse resExpired(String codeMsg, Object data) {
		WebResponse res = new WebResponse();
		res.setCode(ResCode.EXPIRED);
		if (codeMsg == null || "".equals(codeMsg.trim())) {
			res.setMsg("登录过期");
		} else {
			res.setMsg(codeMsg);
		}
		res.setData(data);
		return res;
	}
	public static WebResponse instance(ReturnCodeEnum returnCodeEnum) {
		WebResponse m = new WebResponse();
		m.setCode(returnCodeEnum.getCode());
		m.setMsg(returnCodeEnum.getMsg());

		return m;
	}

	public static WebResponse success(Object data) {
		WebResponse m = instance(ReturnCodeEnum.SUCCESS);
		m.setData(data);
		return m;
	}

	public static WebResponse success(ReturnCodeEnum returnCodeEnum, Object data){
		WebResponse m = instance(returnCodeEnum);
		m.setData(data);
		return m;
	}

	public static WebResponse getResult(int num ){
		if(num > 0){
			return WebResponse.resSuccess("操作成功",null);
		}
		return WebResponse.resFail("操作失败",null);
	}
}
